import axios from 'axios';
import { Base64 } from 'js-base64';


export async function getSupconDatasHDD(day,machinename,batchname,dyeprogname) {
    try {
        console.log(day,machinename,batchname,dyeprogname)
        // 使用模板字符串或参数绑定来构造SQL语句（取决于你的后端API如何处理参数）
        const sqlStatement = "";
        const sql = Base64.encode(sqlStatement);
        // 发送POST请求到后端API，并传递参数
        const response = await axios.post(
            "http://8.134.79.23:34343/sql_actuator/execute",
            {
                "env": 'internet-ms-pe',
                "sql": sql,
            }
        );
        const responseData = response.data.data;
        return responseData;
    } catch (error) {
        console.error('Error fetching data:', error);
        throw error; // 抛出错误以便在组件中处理
    }
}
export function getTest1(url,parameter){
    return axios({
        url: url,
        method: 'get',
        params: parameter
    })
}
export async function getTest2(url,parameter){
    await axios.post(
        url,
        parameter
    )
}

export async function getMachineDetaiPie(day) {
    try {
        if (null == day){
            day = 100;
        }
        const sqlStatement = " SELECT tavg.machinename,tavg.batchname,tavg.dyeprogname,round(sum(tavg.water_loading_rate)/count(1),2) as wlavg_rate from \n" +
            " (SELECT m.machinename,m.batchname,m.dyeprogname,\n" +
            "     (case when m.loading = 0 then 0 else round((m.water_vol4_total_all/m.loading*1000),2) end) as water_loading_rate\n" +
            "  from t_machine_info m \n" +
            "  WHERE m.create_time BETWEEN DATEADD(DAY, -"+day+", GETDATE()) AND GETDATE()\n" +
            "  and m.water_vol4_total_all > 0) tavg\n" +
            "  group by tavg.machinename,tavg.batchname,tavg.dyeprogname";
        const sql = Base64.encode(sqlStatement);
        // 发送POST请求到后端API，并传递参数
        const response = await axios.post(
            "http://8.134.79.23:34343/sql_actuator/execute",
            {
                "env": 'internet-ms-pe',
                "sql": sql,
            }
        );
        const responseData = response.data.data;
        return responseData;
    } catch (error) {
        console.error('Error fetching data:', error);
        throw error;
    }
}

export async function getMachineDetaiLine(day,machinename) {
    try {
        if (null == day){
            day = 100;
        }
        const sqlStatement = "SELECT m.machinename,m.batchname,m.dyeprogname,\n" +
            "     convert(varchar,m.create_time,120) as runtime,\n" +
            "     (case when m.machinestate = '停止' then 0 else 10 end) as machinestate,\n" +
            "     (case when m.machinemode = '单机' then 0 else 20 end) as machinemode,\n" +
            "     75 as standard_line,\n" +
            "     m.water_vol4_total_all,\n" +
            "     m.loading,\n" +
            "     (case when m.loading = 0 then 0 else round((m.water_vol4_total_all/m.loading*1000),2) end) as water_loading_rate\n" +
            "  from t_machine_info m \n" +
            "  WHERE m.create_time BETWEEN DATEADD(DAY, -"+day+", GETDATE()) AND GETDATE()\n" +
            "   and m.machinename = '"+machinename+"'\n" +
            "  order by m.machinename,convert(varchar,m.create_time,120)";
        const sql = Base64.encode(sqlStatement);
        // 发送POST请求到后端API，并传递参数
        const response = await axios.post(
            "http://8.134.79.23:34343/sql_actuator/execute",
            {
                "env": 'internet-ms-pe',
                "sql": sql,
            }
        );
        const responseData = response.data.data;
        return responseData;
    } catch (error) {
        console.error('Error fetching data:', error);
        throw error;
    }
}

export async function getSupconDatasHD(day,machinename) {
    try {
        if (null == day){
            day = 10;
        }
        const sqlStatement = "SELECT m.machinename,m.batchname,m.tubes,CONVERT(varchar(10), m.create_time, 120) as rundate,m.dyeprogname,\n" +
            "             dense_rank() over(partition by m.machinename,CONVERT(varchar(10), m.create_time, 120) order by m.dyeprogname) as t_rank,\n" +
            "             min(CONVERT(varchar, m.create_time, 120)) as starttime,\n" +
            "             max(CONVERT(varchar, m.create_time, 120)) as endtime\n" +
            "             from t_machine_info m \n" +
            "             WHERE m.create_time BETWEEN DATEADD(DAY, -"+day+", GETDATE()) AND GETDATE()\n" +
            "               and m.machinename = '"+machinename+"'\n" +
            "             group by m.machinename,m.batchname,m.tubes,CONVERT(varchar(10), m.create_time, 120),m.dyeprogname  \n" +
            "             order by CONVERT(varchar(10), m.create_time, 120),m.dyeprogname";

        const sql = Base64.encode(sqlStatement);
        // 发送POST请求到后端API，并传递参数
        const response = await axios.post(
            "http://8.134.79.23:34343/sql_actuator/execute",
            {
                "env": 'internet-ms-pe',
                "sql": sql,
            }
        );
        const responseData = response.data.data;
        return responseData;
    } catch (error) {
        console.error('Error fetching data:', error);
        throw error;
    }
}

export async function getSupconDatasH(day) {
    try {
        if (null == day){
            day = 100;
        }
        const sqlStatement = "SELECT t.machinename,t.tubes,count(1) AS count_machinename           \n" +
            "           from (SELECT m.machinename,m.batchname,m.tubes,CONVERT(varchar(10), m.create_time, 120) as rundate,m.dyeprogname,\n" +
            "             dense_rank() over(partition by m.machinename,CONVERT(varchar(10), m.create_time, 120) order by m.dyeprogname) as t_rank,\n" +
            "             min(CONVERT(varchar, m.create_time, 120)) as starttime,\n" +
            "             max(CONVERT(varchar, m.create_time, 120)) as endtime\n" +
            "             from t_machine_info m \n" +
            "             WHERE m.create_time BETWEEN DATEADD(DAY, -"+day+", GETDATE()) AND GETDATE() \n" +
            "             group by m.machinename,m.batchname,m.tubes,CONVERT(varchar(10), m.create_time, 120),m.dyeprogname) t\n" +
            "           group by t.machinename,t.tubes order by t.machinename,t.tubes";
        const sql = Base64.encode(sqlStatement);
        // 发送POST请求到后端API，并传递参数
        const response = await axios.post(
            "http://8.134.79.23:34343/sql_actuator/execute",
            {
                "env": 'internet-ms-pe',
                "sql": sql,
            }
        );
        const responseData = response.data.data;
        return responseData;
    } catch (error) {
        console.error('Error fetching data:', error);
        throw error;
    }
}



export async function expandRowsH(pei_type,cloth_line_name,product_code,product_name,startDate,endDate,outlossrate) {
    try {
        // 使用模板字符串或参数绑定来构造SQL语句（取决于你的后端API如何处理参数）
        const sqlStatement = "SELECT t.product_code,t.product_name,t.degree_of_staining_code, \n" +
            "                        t.degree_of_staining_name,count(1) as detail_count, \n" +
            "                        case when count(1) = 0 then 0 else round(sum(t.loss_rate)/count(1),2) end AS avg_loss_rate, \n" +
            "                        sum(case when t.loss_rate > "+outlossrate+" then 1 else 0 end) AS out_loss_rate,t.t_cloth_line_name as cloth_line_name,t.pei_type \n" +
            "                         FROM (SELECT l.t_dyelot_code,\n" +
            "                          l.t_product_code as product_code,\n" +
            "                          l.t_product_name as product_name,\n" +
            "                          l.t_label_color_name as label_color_name,\n" +
            "                          l.t_fabric_sum as fabric_sum,\n" +
            "                          l.t_fabric_label_weight_sum as fabric_label_weight_sum,\n" +
            "                          l.t_pi_sum as pi_sum,\n" +
            "                          l.t_xima_sum as xima_sum,\n" +
            "                          l.t_kongcha as kongcha,\n" +
            "                          l.t_tube_weight as tube_weight,\n" +
            "                          l.t_loss_rate as loss_rate,\n" +
            "                          l.t_remarks as remarks,\n" +
            "                          l.t_degree_of_staining_code as degree_of_staining_code,\n" +
            "                          l.t_degree_of_staining_name as degree_of_staining_name,\n" +
            "                          FORMAT(l.t_in_date, 'yyyy-MM-dd') as in_date,\n" +
            "                          d.t_cloth_line_name,\n" +
            "                          l.pei_type \n" +
            "                         from t_io_doc_loss l left join t_dyelot_info d on l.t_dyelot_code = d.t_dyelot_code \n" +
            "                        where l.t_in_date BETWEEN CONVERT(datetime, '"+startDate+" 00:00:00') and CONVERT(datetime, '"+endDate+" 23:59:59')  \n" +
            "                           and l.pei_type like '%"+pei_type+"%' and d.t_cloth_line_name like '%"+cloth_line_name+"%' and l.t_product_name like '%"+product_name+"%' and l.t_product_code like '%"+product_code+"%' ) t  \n" +
            "                 where (t.loss_rate<100 or (t.loss_rate=100 and t.product_code='000#'))  \n" +
            "                 GROUP BY t.product_code,t.product_name,t.degree_of_staining_code,t.degree_of_staining_name,t.t_cloth_line_name,t.pei_type ";

        // console.log(sqlStatement)
        const sql = Base64.encode(sqlStatement);
        // console.log(sqlStatement)
        // 发送POST请求到后端API，并传递参数
        const response = await axios.post(
            "http://8.134.79.23:34343/sql_actuator/execute",
            {
                "env": 'internet-ms-pe',
                "sql": sql,
            }
        );
        const responsedatas = response.data.data;
        return responsedatas;
    } catch (error) {
        console.error('Error fetching data:', error);
        throw error; // 抛出错误以便在组件中处理
    }
}
export async function expandRowsExportD(pei_type,cloth_line_name,product_code,product_name,startDate,endDate) {
    try {
        // 使用模板字符串或参数绑定来构造SQL语句（取决于你的后端API如何处理参数）
        const sqlStatement = "SELECT ls.t_dyelot_code as dyelot_code,\n" +
            "                          ls.t_product_code as product_code,\n" +
            "                          ls.t_product_name as product_name,\n" +
            "                          ls.t_degree_of_staining_code as degree_of_staining_code,\n" +
            "                          ls.t_degree_of_staining_name as degree_of_staining_name,\n" +
            "                          ls.t_label_color_name as label_color_name,\n" +
            "                          ls.t_fabric_sum as fabric_sum,\n" +
            "                          ls.t_fabric_label_weight_sum as fabric_label_weight_sum,\n" +
            "                          ls.t_pi_sum as pi_sum,\n" +
            "                          ls.t_xima_sum as xima_sum,\n" +
            "                          ls.t_kongcha as kongcha,\n" +
            "                          ls.t_tube_weight as tube_weight,\n" +
            "                          ls.t_loss_rate as loss_rate,\n" +
            "                          ls.t_remarks as remarks,\n" +
            "                          ls.t_in_date as in_date,\n" +
            "                          pf.workmanship_pay_project_name as workmanshipPayProjects,\n" +
            "                          d.t_cloth_line_name as cloth_line_name\n" +
            "                       from t_io_doc_loss ls left join t_dyelot_info d on ls.t_dyelot_code = d.t_dyelot_code,\n " +
            "                            t_dyelot_production_process_info pf \n" +
            "                       where ls.t_dyelot_code = pf.dyelot_code \n" +
            "                       and ls.t_in_date BETWEEN CONVERT(datetime, '"+startDate+" 00:00:00') and CONVERT(datetime, '"+endDate+" 23:59:59')\n" +
            "                       and ls.pei_type like '%"+pei_type+"%' and d.t_cloth_line_name like '%"+cloth_line_name+"%' and ls.t_product_name like '%"+product_name+"%'  and ls.t_product_code like '%"+product_code+"%' ";
        const sql = Base64.encode(sqlStatement);
        // 发送POST请求到后端API，并传递参数
        const response = await axios.post(
            "http://8.134.79.23:34343/sql_actuator/execute",
            {
                "env": 'internet-ms-pe',
                "sql": sql,
            }
        );

        const responseData = response.data.data;
        return responseData;
    } catch (error) {
        console.error('Error fetching data:', error);
        throw error; // 抛出错误以便在组件中处理
    }
}

export async function expandRowsD(pei_type,cloth_line_name,product_code,degree_of_staining_code,startDate,endDate) {
    try {
        // 使用模板字符串或参数绑定来构造SQL语句（取决于你的后端API如何处理参数）
        const sqlStatement = "SELECT ls.t_dyelot_code as dyelot_code,\n" +
            "              d.t_cloth_line_name as cloth_line_name,\n" +
            "              ls.t_product_code as product_code,\n" +
            "              ls.t_product_name as product_name,\n" +
            "              ls.t_degree_of_staining_code as degree_of_staining_code,\n" +
            "              ls.t_degree_of_staining_name as degree_of_staining_name,\n" +
            "              ls.t_label_color_name as label_color_name,\n" +
            "              ls.t_fabric_sum as fabric_sum,\n" +
            "              ls.t_fabric_label_weight_sum as fabric_label_weight_sum,\n" +
            "              ls.t_pi_sum as pi_sum,\n" +
            "              ls.t_xima_sum as xima_sum,\n" +
            "              ls.t_kongcha as kongcha,\n" +
            "              ls.t_tube_weight as tube_weight,\n" +
            "              ls.t_loss_rate as loss_rate,\n" +
            "              ls.t_remarks as remarks,\n" +
            "              FORMAT(ls.t_in_date, 'yyyy-MM-dd') as in_date, \n" +
            "              pf.workmanship_pay_project_name as workmanshipPayProjects \n" +
            "          from t_io_doc_loss ls left join t_dyelot_info d on ls.t_dyelot_code = d.t_dyelot_code, \n" +
            "               t_dyelot_production_process_info pf \n" +
            "           where ls.t_dyelot_code = pf.dyelot_code \n" +
            "           and ls.pei_type like '%"+pei_type+"%' and d.t_cloth_line_name like '%"+cloth_line_name+"%' and ls.t_product_code = '"+product_code+"' and ls.t_degree_of_staining_code = '"+degree_of_staining_code+"' \n" +
            "           and ls.t_in_date BETWEEN CONVERT(datetime, '"+startDate+" 00:00:00') and CONVERT(datetime, '"+endDate+" 23:59:59')  \n"
        ;
        const sql = Base64.encode(sqlStatement);
        // 发送POST请求到后端API，并传递参数
        const response = await axios.post(
            "http://8.134.79.23:34343/sql_actuator/execute",
            {
                "env": 'internet-ms-pe',
                "sql": sql,
            }
        );

        const responseData = response.data.data;
        return responseData;
    } catch (error) {
        console.error('Error fetching data:', error);
        throw error; // 抛出错误以便在组件中处理
    }
}
